package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.generateRFC;

import javax.swing.JLabel;
import javax.swing.JOptionPane;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.CMSThread;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.generateRFC.subs.DataInputValueTester;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.GenerateRFCPanel;
import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.generateRFCPanel.generateRFCSubs.DatainputPanel;

public class SaveRFCThread extends CMSThread
{
	private DatainputPanel datainputPanel;
	private DataInputValueTester dataInputValueTester;
	private JLabel progress;
	private GenerateRFCPanel generateRFCPanel;
	
	public SaveRFCThread(GenerateRFCPanel generateRFCPanel, JLabel progress)
	{
		this.generateRFCPanel = generateRFCPanel;
		this.progress = progress;
		this.datainputPanel = generateRFCPanel.getDatainputPanel();
		this.dataInputValueTester = new DataInputValueTester(datainputPanel);
	}
	
	public void run()
	{
		saveNewRFC();
	}
	
	private void saveNewRFC()
	{
		this.changeStateofComponents(datainputPanel.getallUserComponents());
		if(this.dataInputValueTester.checkallValuesSmallcheck())
		{
			boolean issaved = this.saveRFCinDB(datainputPanel, progress);
			if(!issaved)
			{
				JOptionPane.showMessageDialog(datainputPanel, "RFC konnte nicht abgespeichert werden");
			}
		}
		else
		{
			JOptionPane.showMessageDialog(datainputPanel, "Mindestens ein Wert ist fehlerhaft");
			progress.setVisible(false);
			generateRFCPanel.revalidate();
		}
		this.changeStateofComponents(datainputPanel.getallUserComponents());
	}
}